<?php

use console\db\Migration;
use yii\db\Query;

/**
 * 更新参与日志-牵手球童
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300200_100045_t_activity_attend_log_enroll extends Migration {

    /**
     * @inheritdoc
     */
    public $tableName = '{{%activity_attend_log}}';

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $query = (new Query())->from('{{%activity_enroll}}')->andWhere([
            '!=', 'status', -1
        ])->orderBy([
            'inputtime' => SORT_ASC
        ]);
        $this->handleProgress($query, [$this, 'evenDule'], null, Yii::$app->olddb);
    }

    /**
     * 处理结果
     * @param Migration $migration
     * @param array $rows
     */
    public static function evenDule($migration, $rows) {
        $connect = $migration->db->createCommand();
        $items = [];
        foreach ($rows as $row) {
            $items[] = [
                'activity_id' => $row['actid'],
                'user_id' => $row['uid'],
                'created_at' => $row['inputtime'],
                'updated_at' => $row['updatetime'],
            ];
        }
        if (empty($items)) {
            return;
        }
        $connect->batchInsert('{{%activity_attend_log}}', [
            'activity_id',
            'user_id',
            'created_at',
            'updated_at',
        ], $items)->execute();
    }

}
